#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
enum spyval
{
    UNSPYED, //未被监控
    SPYED,   //已被监控
    SPYER    //监控者
};
class Solution
{
public:
    int cnt = 0;
    int dfs(TreeNode *root)
    {
        if (root == nullptr)
            return SPYED;
        int left = dfs(root->left), right = dfs(root->right);
        if (left == UNSPYED || right == UNSPYED)
        {
            cnt++;
            return SPYER;
        }
        else if (left == SPYER || right == SPYER)
            return SPYED;
        else
            return UNSPYED;
    }
    int minCameraCover(TreeNode *root)
    {
        if (dfs(root) == UNSPYED)cnt++;
        return cnt;
    }
};